package com.sci99.ex.vm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Category;
import com.sci99.ex.model.Course;
import com.sci99.ex.model.Exam;
import com.sci99.ex.service.CategoryService;
import com.sci99.ex.service.CourseService;
import com.sci99.ex.service.ExamService;

public class ExamVM {
	Logger log = Logger.getLogger(ExamVM.class);

	private List<Category> cateList;
	private Category selCate;
	private List<Course> courList;
	private Course selCour;
	private List<Exam> examList;
	private Exam selExam;

	public List<Category> getCateList() {
		return cateList;
	}

	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}

	public Category getSelCate() {
		return selCate;
	}

	public void setSelCate(Category selCate) {
		this.selCate = selCate;
	}

	public List<Course> getCourList() {
		return courList;
	}

	public void setCourList(List<Course> courList) {
		this.courList = courList;
	}

	public Course getSelCour() {
		return selCour;
	}

	public void setSelCour(Course selCour) {
		this.selCour = selCour;
	}

	public List<Exam> getExamList() {
		return examList;
	}

	public void setExamList(List<Exam> examList) {
		this.examList = examList;
	}

	public Exam getSelExam() {
		return selExam;
	}

	public void setSelExam(Exam selExam) {
		this.selExam = selExam;
	}

	@AfterCompose
	public void doAfterCompose() {
		try {
			cateList = CategoryService.getCategoryActiveList();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	@Command
	@NotifyChange("courList")
	public void selCate() {
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("category_id", selCate.getId());
		try {
			courList = CourseService.getCourseList(args);
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
	}

	@Command
	@NotifyChange("examList")
	public void search() {
		if (selCour == null) {
			Messagebox.show("请选择课程");
			return;
		}
		try {
			examList = ExamService.getExams(selCour.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
	}

	@Command
	@NotifyChange("selExam")
	public void add(@BindingParam("win") Window edWin) {
		if (selCour == null) {
			Messagebox.show("请选择课程");
			return;
		}
		selExam = new Exam();
		selExam.setCourse_id(selCour.getId());
		edWin.doModal();
	}

	@Command
	public void edit(@BindingParam("win") Window edWin) {
		edWin.doModal();
	}

	@Command
	@NotifyChange("examList")
	public void save(@BindingParam("win") Window edWin) {
		try {
			ExamService.saveExam(selExam);
			examList = ExamService.getExams(selCour.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
		edWin.setVisible(false);
	}

	@Command
	@NotifyChange("examList")
	public void del(@BindingParam("win") Window edWin) {

		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		try {
			ExamService.delExam(selExam);
			examList = ExamService.getExams(selCour.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
		edWin.setVisible(false);
	}

	@Command
	@NotifyChange("examList")
	public void close(@BindingParam("win") Window edWin) {
		try {
			examList = ExamService.getExams(selCour.getId());
		} catch (Exception e) {
			Messagebox.show(Msg.DbFail);
			log.error("", e);
		}
		edWin.setVisible(false);
	}
}
